<?php
use Carbon\Carbon;

/**
 * 验证手机号
 */
function checkMobile($mobile)
{
    return preg_match('/^1[3-9]\d{9}$/', $mobile);
}

function positionType($type)
{
    switch ($type) {
        case 'front':
            $number = 1;

            break;
        case 'rear':
            $number = 2;

            break;
        case 'left':
            $number = 3;

            break;
        case 'right':
            $number = 4;

            break;
        case 'top':
            $number = 5;

            break;
        default:
            $number = 6;
    }

    return $number;
}

function webOrderStatus($status_font)
{
    switch ($status_font) {
        case 'pending':
            $status = ORDER_WAIT_MAINTENANCE;

            break; 
        case 'waiting_disassembly':
            $status = ORDER_WAIT_PLATE_REMOVE;

            break;
        case 'disassembly':
            $status = ORDER_PLATE_REMOVE;

            break;
        case 'waiting_shaping':
            $status = ORDER_WAIT_PLATE_RESHAPING;

            break;
        case 'shaping':
            $status = ORDER_PLATE_RESHAPING;

            break;
        case 'waiting_priming':
            $status = ORDER_WAIT_PAINT;

            break;
        case 'priming':
            $status = ORDER_PAINT;

            break;
        case 'waiting_painting':
            $status = ORDER_WAIT_SPRAY_PAINT;

            break;
        case 'painting':
            $status = ORDER_SPRAY_PAINT;

            break;
        case 'waiting_reassembly':
            $status = ORDER_WAIT_PLATE_INSTALL;

            break;
        case 'reassembly':
            $status = ORDER_PLATE_INSTALL;

            break;
        case 'waiting_polishing':
            $status = ORDER_WAIT_POLISH;

            break;
        case 'polishing':
            $status = ORDER_POLISH;

            break;
        case 'waiting_electrical':
            $status = ORDER_WAIT_ELECTROMECHANICAL_INSTALL;

            break;
        case 'electrical':
            $status = ORDER_ELECTROMECHANICAL_INSTALL;

            break;
        case 'inspection':
            $status = ORDER_QUALITY_TESTING;

            break;
        case 'settlement':
            $status = ORDER_WAIT_PAY;

            break;
        default:
            $status = ORDER_WAIT_PAY;

            break;
    }

    return $status;
}

function processesArchivesMerge($processes_data)
{
    $keyedArray2 = [];
    foreach ($processes_data as $item) {
        $keyedArray2[$item['processes_archives_id']] = $item;
    }

    $processes = [
        ['id' => 1, 'title' => '待维修', 'status' => 'wait'],
        ['id' => 2, 'title' => '钣金拆解', 'status' => 'wait'],
        ['id' => 3, 'title' => '钣金整形', 'status' => 'wait'],
        ['id' => 4, 'title' => '油漆做底', 'status' => 'wait'],
        ['id' => 5, 'title' => '喷漆', 'status' => 'wait'],
        ['id' => 6, 'title' => '钣金装复', 'status' => 'wait'],
        ['id' => 7, 'title' => '抛光', 'status' => 'wait'],
        ['id' => 8, 'title' => '机电装配', 'status' => 'wait'],
        ['id' => 9, 'title' => '洗车、质检', 'status' => 'wait'],
        ['id' => 10, 'title' => '待结算', 'status' => 'wait']
    ];

    foreach ($processes as &$item1) {
        $id = $item1['id'];

        if (isset($keyedArray2[$id])) {
            // 合并数组2中的匹配项到数组1当前元素
            $item1 = array_merge($item1, $keyedArray2[$id]);

            if ($keyedArray2[$id]['repair_status'] == 1 || $keyedArray2[$id]['repair_status'] == 2) {
                $item1['status'] = 'active';
            } else {
                $item1['status'] = 'completed';
            }
        }
    }

    unset($item1); // 取消引用

    return $processes;

}

function componentTitle($type, $multi_type = 1)
{
    if ($multi_type == 1) {
        switch ($type) {
            case 1:
                $title = 'front';

                break;
            case 2:
                $title = 'rear';

                break;
            case 3:
                $title = 'left';

                break;
            case 4:
                $title = 'right';

                break;
            case 5:
                $title = 'top';

                break;
            default:
                $title = 'other';
        }
    } else {
        switch ($type) {
            case 1:
                $title = '前部';

                break;
            case 2:
                $title = '后部';

                break;
            case 3:
                $title = '左侧';

                break;
            case 4:
                $title = '右侧';

                break;
            case 5:
                $title = '顶部';

                break;
            default:
                $title = '其他';
        }
    }

    return $title;
}

function damageLevelType($type)
{
    switch ($type) {
        case 'light':
            $number = 1;

            break;
        case 'medium':
            $number = 2;

            break;
        case 'heavy':
            $number = 3;

            break;
        case 'new':
            $number = 4;

            break;
        default:
            $number = $type;
    }

    return $number;
}

function damageLevelTypeTitle($type)
{
    switch ($type) {
        case 1:
            $number = 'light';

            break;
        case 2:
            $number = 'medium';

            break;
        case 3:
            $number = 'heavy';

            break;
        case 4:
            $number = 'new';

            break;
        default:
            $number = 'light';
    }

    return $number;
}

function repairStatusTitle($status)
{
    switch ($status) {
        case 1:
            $title = '未开始';

            break;
        case 2:
            $title = '维修中';

            break;
        case 3:
            $title = '维修完成';

            break;
        default:
            $title = '维修中';
    }

    return $title;
}

function stopReworkTitle($type)
{
    switch ($type) {
        case 1:
            $title = '中断';

            break;
        case 2:
            $title = '返工';

            break;
        default:
            $title = '中断';
    }

    return $title;
}

function componentType($type)
{
    switch ($type) {
        case 'paint':
            $type_number = 1;

            break;
        default:
            $type_number = 2;
    }

    return $type_number;
}

function systemUserIds()
{
    return [1, 7, 9];
}

function dyXcwyPackageIds()
{
    return [28, 34];
}

function dyXcwyXsPackageIds()
{
    return [28];
}

function dyXcwyShPackageIds()
{
    return [34];
}

function dyJcbyPackageIds()
{
    return [29, 30];
}

function dyBpqPackageIds()
{
    return [31];
}

function zzBpqPackageIds()
{
    return [32];
}

function dfBpqPackageIds()
{
    return [33];
}

function bpqIds()
{
    return [31, 32, 33];
}

function carType()
{
    return ['1' => '轿车', '2' => 'SUV', '3' => '新能源', '4' => 'MPV'];
}

function statusSwitch()
{
    return ['1' => '开启', '2' => '关闭'];
}

function statusSwitchTitle($status)
{
    return $status == 1 ? '开启' : '关闭';
}

function path2url($path)
{
    $url = '';

    if ($path) {
        if (substr($path, 0, 1) == '/') {
            $url = env('QN_APP_URL').$path;
        } else {
            $url = env('QN_APP_URL').'/'.$path;
        }

    }

    return $url;
}

function pathsUrl($path)
{
    $url = [];

    if ($path) {
        foreach ($path as $key => $value) {
            if ($value) {
                if (substr($value['url'], 0, 1) == '/') {
                    $url[$key]['url'] = env('QN_APP_URL').$value['url'];
                } else {
                    $url[$key]['url'] = env('QN_APP_URL').'/'.$value['url'];
                }

            }
        }
    }

    return $url;
}

function successMessage($data = [], $message = '成功', $code = 200)
{
    return ['code' => $code, 'data' => $data, 'message' => $message];
}

function failureMessage($message = '失败', $code = 400)
{
    return ['code' => $code, 'message' => $message];
}

/**
 * 验证是否本地环境
 * @return bool
 */
function isLocalHost()
{
    if (env('APP_ENV') == 'local') {
        return true;
    } else {
        return false;
    }
}

/**
 * 获取真实IP地址
 */
function getRealIp()
{
    $ip = false;
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
        $ip = getenv("HTTP_CLIENT_IP");
    } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
        $ips = explode(", ", getenv("HTTP_X_FORWARDED_FOR"));
        if ($ip) {
            array_unshift($ips, $ip);
            $ip = false;
        }
        $ipscount = count($ips);
        for ($i = 0; $i < $ipscount; $i++) {
            if (!preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i])) {
                $ip = $ips[$i];
                break;
            }
        }
    } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
        $ip = getenv("REMOTE_ADDR");
    } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
        $ip = $_SERVER['REMOTE_ADDR'];
    } else {
        $ip = "unknown";
    }

    return isIp($ip) ? $ip : "unknown";
}

/**
 * 检查是否是合法的IP
 */
function isIp($ip)
{
    if (preg_match('/^((\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)(?:\.(\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)){3})$/', $ip)) {
        return true;
    } else {
        return false;
    }
}

function curlRequest($url, $data = '')
{
    $ch = curl_init();
    $header = array("Accept-Charset: utf-8");
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $tmpInfo = curl_exec($ch);

    if (curl_errno($ch)) {
        return false;
    } else {
        return $tmpInfo;
    }
}

function printLog($filename, $data)
{
    if (isLocalHost()) {
        $file_path = "/phpstudy_pro/WWW/xyh-platform";
    } else {
        $file_path = "/www/wwwroot/www.sdxinyuehai.com";
    }

    file_put_contents($file_path."/storage/logs/".$filename."-". date("Y-m-d", time()) . ".txt",
        "\r\n -------------".date("Y-m-d H:i:s", time())."---------------- \r\n".$data."\r\n -------------".date("Y-m-d H:i:s", time())."---------------- \r\n",
        FILE_APPEND
    );
}

function formatTimeDifference($time1, $time2)
{
    $start = Carbon::parse($time1);
    $end = Carbon::parse($time2);
    $diff = $start->diff($end);

    if ($diff->days < 1) {
        return "{$diff->h}时{$diff->i}分";
    } else {
        return "{$diff->d}天{$diff->h}时{$diff->i}分";
    }
}